<?php
require_once 'common/init.php';
require_once 'config/conf.inc.php';

function main()
{
	global $tpl;
	$action = trim($_POST['act']);

	$user = LoginManager::get_login_user();
	if (empty($user))
	{
		$tpl->assign("login_status", LOGINSTATUS_OFF);
		$ret = $tpl->display('user.html');
		return;
	}
	else
	{
		$tpl->assign("login_status", LOGINSTATUS_ON);
		$tpl->assign("login_user", $user);
	}
	
	$email = AccountManager::get_email($user);
	$tpl->assign("no_err", true);
	$tpl->assign("action", $action);
	$tpl->assign("email", $email);
	if ($action == "act_edit_profile")
	{
		$new_email = trim($_POST['email']);
		if (!check_is_email($new_email))	
			$tpl->assign("no_err", false);
		else
			$ret = AccountManager::modify_email($user, $new_email, $err);
	
		if (!$ret)	
			$tpl->assign("no_err", false);
		else
			$tpl->assign("email", $new_email);
	}
	else if ($action == "act_edit_password")
	{
		$type = LoginManager::get_login_type();
		$pass = trim($_POST['old_password']);
		$new_pass = trim($_POST['new_password']);
		$ret = AccountManager::check_login($user, $pass, $type, $err);
		if ($ret)
		{
			$r = AccountManager::modify_password($user, $new_pass, $err);
			if (!$r)	$tpl->assign("no_err", false);
		}
		else
		{
			$tpl->assign("no_err", false);
		}
	}
	$ret = $tpl->display('user.html');

	return;	
}

main();
?>